/////////////////////////////////////////////////////////////////////////////
// Name:        Function_Diag.cpp
// Purpose:     Manager of the Function item
// Author:      Matthew Gong
// Created:     05/08/2005
// Copyright:   (c) Matthew Gong
// Licence:     GPL licence
/////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
// #pragma implementation
#endif

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

// -*- C++ -*- generated by wxGlade 0.3.5.1 on Sat May  7 21:20:49 2005
#include <wx/datetime.h>

#include "Function_Diag.h"
#include "TreeItem.h"

BEGIN_EVENT_TABLE(FunctionDialog, wxDialog)
  EVT_COMBOBOX(ID_FUNC_TYPE, FunctionDialog::OnSelectType)
END_EVENT_TABLE()

FunctionDialog::FunctionDialog(wxWindow* parent, int id, const wxString& title, const wxPoint& pos, const wxSize& size, long style):
    wxDialog(parent, id, title, pos, size, style)
{
    // begin wxGlade: FunctionDialog::FunctionDialog
    label_type = new wxStaticText(this, -1, _("Type:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    const wxString combo_box_type_choices[] = {
        wxT("function"),
        wxT("product"),
        wxT("difference"),
        wxT("sum"),
        wxT("quotient"),
        wxT("pow"),
        wxT("abs"),
        wxT("sin"),
        wxT("cos"),
        wxT("tan"),
        wxT("asin"),
        wxT("acos"),
        wxT("atan"),
        wxT("derivative"),
        wxT("integral"),
        wxT("lt"),
        wxT("le"),
        wxT("ge"),
        wxT("gt"),
        wxT("eq"),
        wxT("ne"),
        wxT("and"),
        wxT("or"),
        wxT("not"),
        wxT("ifthen")
    };
    combo_box_type = new wxComboBox(this, ID_FUNC_TYPE, wxT(""), wxDefaultPosition, wxDefaultSize, 25, combo_box_type_choices, wxCB_DROPDOWN|wxCB_READONLY);
    label_name = new wxStaticText(this, -1, _("Name:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    text_ctrl_name = new wxTextCtrl(this, -1, wxT(""));
    label_dscp = new wxStaticText(this, -1, _("Description:"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
    text_ctrl_dscp = new wxTextCtrl(this, -1, wxT(""));
    button_ok = new wxButton(this, wxID_OK, _("OK"));
    button_cancel = new wxButton(this, wxID_CANCEL, _("Cancel"));

    set_properties();
    do_layout();
    // end wxGlade
}


void FunctionDialog::set_properties()
{
    // begin wxGlade: FunctionDialog::set_properties
    label_type->SetMinSize(wxSize(100, -1));
    label_type->SetToolTip(_("Type"));
    combo_box_type->SetMinSize(wxSize(100, -1));
    combo_box_type->SetSelection(-1);
    label_name->SetMinSize(wxSize(100, -1));
    text_ctrl_name->SetMinSize(wxSize(200, -1));
    label_dscp->SetMinSize(wxSize(100, -1));
    text_ctrl_dscp->SetMinSize(wxSize(350, -1));
    // end wxGlade
}


void FunctionDialog::do_layout()
{
    // begin wxGlade: FunctionDialog::do_layout
    wxBoxSizer* sizer_1 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer* sizer_5 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_4 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_3 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer* sizer_2 = new wxBoxSizer(wxHORIZONTAL);
    sizer_2->Add(label_type, 0, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_2->Add(combo_box_type, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
    sizer_1->Add(sizer_2, 1, wxEXPAND, 0);
    sizer_3->Add(label_name, 0, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_3->Add(text_ctrl_name, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
    sizer_1->Add(sizer_3, 1, wxEXPAND, 0);
    sizer_4->Add(label_dscp, 0, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5);
    sizer_4->Add(text_ctrl_dscp, 1, wxALL|wxALIGN_CENTER_VERTICAL, 5);
    sizer_1->Add(sizer_4, 1, wxEXPAND, 0);
    sizer_5->Add(button_ok, 1, wxALL, 10);
    sizer_5->Add(button_cancel, 1, wxALL, 10);
    sizer_1->Add(sizer_5, 1, wxEXPAND, 0);
    SetAutoLayout(true);
    SetSizer(sizer_1);
    sizer_1->Fit(this);
    sizer_1->SetSizeHints(this);
    Layout();
    // end wxGlade
}

void FunctionDialog::Load(const TreeItemData_function * func)
{
  combo_box_type->SetStringSelection(func->type);
  if (func->Type == TreeItemData_function::eTopLevel)
  {
    text_ctrl_name->SetValue(func->name);
    text_ctrl_name->Enable(true);
  }
  else
  {
    text_ctrl_name->Enable(false);  
  }
  text_ctrl_dscp->SetValue(func->description);
}

void FunctionDialog::Save(TreeItemData_function * func)
{
  wxString tmp = combo_box_type->GetStringSelection();
  if (tmp.IsEmpty())
    return;
  func->type = tmp;
  if ( func->type == wxT("function"))
  {
    func->Type = TreeItemData_function::eTopLevel;
    func->name = text_ctrl_name->GetValue();
    func->tree->SetItemText(func->GetId(), func->name);
    func->tree->SetItemImage(func->GetId(), 24);
    func->tree->SetItemImage(func->GetId(), 24, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("product"))
  {
    func->Type = TreeItemData_function::eProduct;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 10);
    func->tree->SetItemImage(func->GetId(), 10, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("difference"))
  {
    func->Type = TreeItemData_function::eDifference;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 11);
    func->tree->SetItemImage(func->GetId(), 11, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("sum"))
  {
    func->Type = TreeItemData_function::eSum;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 12);
    func->tree->SetItemImage(func->GetId(), 12, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("quotient"))
  {
    func->Type = TreeItemData_function::eQuotient;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 13);
    func->tree->SetItemImage(func->GetId(), 13, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("pow"))
  {
    func->Type = TreeItemData_function::ePow;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 14);
    func->tree->SetItemImage(func->GetId(), 14, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("abs"))
  {
    func->Type = TreeItemData_function::eAbs;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 15);
    func->tree->SetItemImage(func->GetId(), 15, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("sin"))
  {
    func->Type = TreeItemData_function::eSin;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 16);
    func->tree->SetItemImage(func->GetId(), 16, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("cos"))
  {
    func->Type = TreeItemData_function::eCos;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 17);
    func->tree->SetItemImage(func->GetId(), 17, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("tan"))
  {
    func->Type = TreeItemData_function::eTan;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 18);
    func->tree->SetItemImage(func->GetId(), 18, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("asin"))
  {
    func->Type = TreeItemData_function::eASin;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 19);
    func->tree->SetItemImage(func->GetId(), 19, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("acos"))
  {
    func->Type = TreeItemData_function::eACos;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 20);
    func->tree->SetItemImage(func->GetId(), 20, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("atan"))
  {
    func->Type = TreeItemData_function::eATan;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 21);
    func->tree->SetItemImage(func->GetId(), 21, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("derivative"))
  {
    func->Type = TreeItemData_function::eDerivative;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 22);
    func->tree->SetItemImage(func->GetId(), 22, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("integral"))
  {
    func->Type = TreeItemData_function::eIntegral;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 23);
    func->tree->SetItemImage(func->GetId(), 23, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("lt"))
  {
    func->Type = TreeItemData_function::eLT;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 36);
    func->tree->SetItemImage(func->GetId(), 36, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("le"))
  {
    func->Type = TreeItemData_function::eLE;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 37);
    func->tree->SetItemImage(func->GetId(), 37, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("gt"))
  {
    func->Type = TreeItemData_function::eGT;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 38);
    func->tree->SetItemImage(func->GetId(), 38, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("ge"))
  {
    func->Type = TreeItemData_function::eGE;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 39);
    func->tree->SetItemImage(func->GetId(), 39, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("eq"))
  {
    func->Type = TreeItemData_function::eEQ;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 40);
    func->tree->SetItemImage(func->GetId(), 40, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("ne"))
  {
    func->Type = TreeItemData_function::eNE;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 41);
    func->tree->SetItemImage(func->GetId(), 41, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("and"))
  {
    func->Type = TreeItemData_function::eAND;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 42);
    func->tree->SetItemImage(func->GetId(), 42, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("or"))
  {
    func->Type = TreeItemData_function::eOR;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 43);
    func->tree->SetItemImage(func->GetId(), 43, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("not"))
  {
    func->Type = TreeItemData_function::eNOT;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 44);
    func->tree->SetItemImage(func->GetId(), 44, wxTreeItemIcon_Selected);
  }
  else if  ( func->type == wxT("ifthen"))
  {
    func->Type = TreeItemData_function::eIFTHEN;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), 45);
    func->tree->SetItemImage(func->GetId(), 45, wxTreeItemIcon_Selected);
  }
  else
  {
    func->Type = TreeItemData_function::eUndef;
    func->tree->SetItemText(func->GetId(), func->type);
    func->tree->SetItemImage(func->GetId(), -1);
    func->tree->SetItemImage(func->GetId(), -1, wxTreeItemIcon_Selected);
  }
  wxString dscp = text_ctrl_dscp->GetValue();
  if (!dscp.IsEmpty())
  {
    func->description = dscp;
    if (func->Type == TreeItemData_function::eTopLevel)
    {
      func->tree->SetItemText(func->GetId(), func->name + wxT("(") + dscp + wxT(")"));
    }
    else
    {
      func->tree->SetItemText(func->GetId(), func->type + wxT("(") + dscp + wxT(")"));
    }
  }
}

void FunctionDialog::OnSelectType(wxCommandEvent & event)
{
  if (combo_box_type->GetValue() == wxT("function"))
  {
    text_ctrl_name->Enable(true);
    if (text_ctrl_name->GetValue().IsEmpty())
    {
      wxWindow * p = GetParent();
      if (p)
      {
        TreeItemEvtHandler * handler = dynamic_cast<TreeItemEvtHandler *>(p->GetEventHandler());
        if (handler)
        {
          text_ctrl_name->SetValue(handler->FuncPrefix + wxString::Format(wxT("%lu"), wxDateTime::Now().GetTicks()));
        }
        else
        {
          text_ctrl_name->SetValue(wxString::Format("function/func%lu", wxDateTime::Now().GetTicks()));
        }
      }
      else
      {
        text_ctrl_name->SetValue(wxString::Format("function/func%lu", wxDateTime::Now().GetTicks()));
      }
    }
  }
  else
  {
    text_ctrl_name->Enable(false);
  }
}


